﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MessageController : MonoBehaviour
{
    public static MessageController Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<MessageController>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("MessageController");
                _Instance = obj.AddComponent<MessageController>();
            }
            return _Instance;
        }
        set
        {
            _Instance = value;
        }
    }
    private static MessageController _Instance;
    private Transform canvasTransform;
    private Transform CanvasTransform
    {
        get
        {
            if (canvasTransform == null)
            {
                canvasTransform = GameObject.Find("Canvas").transform;
            }
            return canvasTransform;
        }
    }

    public GameObject prefabText;
    public List<Text> texts = new List<Text>();


    public float slowRiseSpeed = 150f;
    public float quickRiseSpeed = 200f;
    public float gap = 35f;
    public int maxNum = 3;

    private float topLocalHeight = -1f;

    void Awake()
    {
        prefabText = (GameObject)Resources.Load("Prefabs/UIPanel/Message");
    }

    void Update()
    {

        while (texts.Count > 0 && texts[0] == null)
        {
            texts.RemoveAt(0);
        }
        if (texts.Count > 0)
        {
            float delta;
            
            if (texts.Count <= maxNum)
            {
                delta = Time.unscaledDeltaTime * slowRiseSpeed;     
            }
            else
            {
                delta = Time.unscaledDeltaTime * quickRiseSpeed;
                for (int i = 0; i < texts.Count - maxNum; i++)
                {
                    texts[i].GetComponent<UIFade>()._opening = false;
                    texts[i].GetComponent<UIFade>()._closing = true;
                }
            }

            float nowTopHeight = texts[0].rectTransform.localPosition.y;
            float riseHeight = Mathf.Min(topLocalHeight - nowTopHeight, delta);
            if (riseHeight <= 0)
                return;
            for (int i = 0; i < texts.Count; i++)
            {
                texts[i].rectTransform.localPosition += new Vector3(0, riseHeight, 0);
            }
        }
    }

    public void SendMsg(string msg)
    {
        //Debug.Log("sendMsg " + msg);
        Text text = Instantiate(prefabText, CanvasTransform,false).GetComponent<Text>();
        if (topLocalHeight < 0)
        {
            topLocalHeight = text.rectTransform.localPosition.y;
        }
        if (texts.Count > 0)
        {
            text.transform.localPosition = texts[texts.Count - 1].transform.localPosition;
            text.transform.localPosition -= new Vector3(0, gap, 0); 
        }
        text.text = msg;
        
        texts.Add(text);
    }

}
